package com.timebank.score.service.impl;

import com.timebank.common.core.web.pojo.AjaxResult;
import com.timebank.common.security.utils.SecurityUtils;
import com.timebank.score.mapper.ScoreTaskLogMapper;
import com.timebank.score.pojo.entity.ScoreTaskLog;
import com.timebank.score.service.ScoreTaskLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 积分任务记录 实现类
 * @author shiliuyinzhen
 */
@Service
public class ScoreTaskLogServiceImpl implements ScoreTaskLogService {

    @Resource
    private ScoreTaskLogMapper scoreTaskLogMapper;

    /**
     * 查询任务记录列表
     *
     * @param scoreTaskLog
     * @return
     */
    @Override
    public List<ScoreTaskLog> getScoreTaskLogList(ScoreTaskLog scoreTaskLog) {
        //设置用户id
        scoreTaskLog.setUserId(SecurityUtils.getUserId());
        List<ScoreTaskLog> list = scoreTaskLogMapper.getScoreTaskLogList(scoreTaskLog);
        return list;
    }

    /**
     * 新增积分任务记录
     * @param scoreTaskLog
     * @return
     */
    @Override
    public AjaxResult addScoreTaskLog(ScoreTaskLog scoreTaskLog) {
        //查询积分任务记录是否存在
        ScoreTaskLog scoreTaskLog1 = ScoreTaskLog.builder()
                .taskId(scoreTaskLog.getTaskId())
                .userId(scoreTaskLog.getUserId())
                .build();
        ScoreTaskLog scoreTaskLog2 = scoreTaskLogMapper.getScoreTaskLog(scoreTaskLog1);
        //如果存在，更新任务记录
        if (scoreTaskLog2 != null){
            return scoreTaskLogMapper.updateScoreTaskLog(scoreTaskLog) > 0 ?
                    AjaxResult.success("新增积分任务记录成功") : AjaxResult.error("新增积分任务记录失败");
        }
        //如果不存在，新增任务记录
        return scoreTaskLogMapper.addScoreTaskLog(scoreTaskLog) > 0 ?
                AjaxResult.success("新增积分任务记录成功") : AjaxResult.error("新增积分任务记录失败");
    }
}
